package com.sitanyun.merchant.guide.frament.view.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.VerPhoneCodeBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.CodeEditView;
import com.sitanyun.merchant.guide.weiht.CountDownTimerUtils;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 获取验证码
 */
public class VerifycodeActivity extends BaseActivity {

    @BindView(R.id.sms_message)
    TextView smsMessage;
    @BindView(R.id.codeEditView)
    CodeEditView codeEditView;
    @BindView(R.id.error_sms)
    TextView errorSms;
    @BindView(R.id.sms_time)
    TextView smsTime;

    @Override
    public int getLayoutId() {
        return R.layout.activity_verifycode;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("安全确认");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Override
    public void initView() {
        smsMessage.setText("我们已经向" + SharedPreferenceUtil.getStringData("phone") + "发送验证码");
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(smsTime, 60000, 1000);
        mCountDownTimerUtils.start();
        initokhttps();
        codeEditView.setOnCompleteListener(new CodeEditView.OnCompleteListener() {
            @Override
            public void onComplete(String content) {
                //  验证验证码
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("target", SharedPreferenceUtil.getStringData("phone"));
                hashMap.put("code", content);
                OkHttpUtils.postString()
                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                        .url(Urls.checkverifycode)
                        .content(new Gson().toJson(hashMap))
                        .build().execute(new StringCallbacks() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        VerPhoneCodeBean baseEntity = new Gson().fromJson(response, VerPhoneCodeBean.class);
                        if (baseEntity.getCode() == 0) {
                            initdrawing();
                        } else {
                            codeEditView.clearText();
                            errorSms.setVisibility(View.VISIBLE);
                            errorSms.setText(baseEntity.getMsg()+"");
                        }

                    }
                });
            }
        });
//        codeEditView.setOnInputEndCallBack(new CodeEditView.inputEndListener() {
//            @Override
//            public void input(String text) { //输入完毕后的回调
//                //  验证验证码
//                HashMap<String, String> hashMap = new HashMap<String, String>();
//                hashMap.put("target", SharedPreferenceUtil.getStringData("phone"));
//                hashMap.put("code", text);
//                OkHttpUtils.postString()
//                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
//                        .addHeader("Content-Type", "application/json")
//                        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
//                        .url(Urls.checkverifycode)
//                        .content(new Gson().toJson(hashMap))
//                        .build().execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//
//                    }
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        VerPhoneCodeBean baseEntity = new Gson().fromJson(response, VerPhoneCodeBean.class);
//                        if (baseEntity.getCode() == 0) {
//                            initdrawing();
//                        } else {
//                            codeEditView.clearText();
//                            errorSms.setVisibility(View.VISIBLE);
//                            errorSms.setText("验证码错误，请重新输入");
//                        }
//
//                    }
//                });
//
//            }

//            @Override
//            public void afterTextChanged(String text) { //输入内容改变后的回调
//            }
 //       });

    }

    //提现
    private void initdrawing() {
        OkHttpUtils.get().url(Urls.drawing) .addHeader("Content-Type", "application/json")
        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
        .addParams("type","1").addParams("cardId",getIntent().getStringExtra("cardid"))
        .build().execute(new StringCallbacks() {
    @Override
    public void onError(Call call, Exception e, int id) {

    }

    @Override
    public void onResponse(String response, int id) {
        VerPhoneCodeBean verPhoneCodeBean=   new Gson().fromJson(response,VerPhoneCodeBean.class);
               if (verPhoneCodeBean.getCode()==0){
                startActivity(new Intent(VerifycodeActivity.this,SuccesActivity.class).putExtra("types","1"));
              finish();
               }else {
           startActivity(new Intent(VerifycodeActivity.this,SuccesActivity.class).putExtra("types","2"));
              finish();
               }
    }
});

    }

    //获取验证码
    private void initokhttps() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("target", SharedPreferenceUtil.getStringData("phone"));

        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .url(Urls.getverifycode)
                .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                VerPhoneCodeBean baseEntity = new Gson().fromJson(response, VerPhoneCodeBean.class);
                if (baseEntity.getCode() == 0) {
                    // ToastUtil.showToast(VerifycodeActivity.this,"发送成功");
                } else {
                    ToastUtil.showToast(VerifycodeActivity.this, "发送信息失败");
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.sms_time)
    public void onViewClicked() {
        if (smsTime.getText().equals("重新获取验证码")) {
            initokhttps();
            CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(smsTime, 60000, 1000);
            mCountDownTimerUtils.start();
        }

    }
}
